package zhouyang;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

class MyTask extends RecursiveTask<Integer>{
    private static  final Integer ADJUST_VALUE=10;
    private int begin;
    private  int end;
    private int result;

    public MyTask(int begin, int end) {
        this.begin = begin;
        this.end = end;
    }

    @Override
    protected Integer compute() {
        if ((end-begin)<=ADJUST_VALUE){
            for (int i = begin; i <=end ; i++) {
                result+=i;
            }
        }else {
            int middle = (end +begin)/2;
            MyTask task1 = new MyTask(begin,middle);
            MyTask task2 = new MyTask(middle+1,end);
            task1.fork();
            task2.fork();
            result = task1.join()+task2.join();
        }
        return result;
    }
}
public class ForkJoinDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
       MyTask myTask = new MyTask(0,1<<1001);
        ForkJoinPool threadPool = new ForkJoinPool();
        ForkJoinTask<Integer> forkJoinTask =threadPool.submit(myTask);
        System.out.println(forkJoinTask.get());//131328
        threadPool.shutdown();
//        System.out.println(1<<30);
        System.out.println(1<<4);//16
        System.out.println(1<<100);//16，jvm把100当成了2进制
        System.out.println(3<<4);//48  3 * 2的4次方
        System.out.println(4<<3);//32  4 * 2的3次方
        System.out.println(1001<<1);//2002
    }
}
